
app.ajax(
	'/Member/Shop/cash_withdrawal_account_edit',
	null,
	function(res){
		if(res.code == 200){
			app.tpl($('.acco-type'),'tpl-acco-type',res);	
			
				var data=window.sessionStorage.getItem('shop_bindAccount');
				data = JSON.parse(data);
				var types = data.type;
				var ids = 0;
				if(data.type == 1){
					header.init('修改账户');
					data =data.data1;
					ids = data.id;
					$('#user-name').val(data.account_name);
					$('#user-num').val(data.account_number);
					$('#account-mess').val(data.account_bank_addr);
					if(data.isdefault){
						$('#switch').find('mui-switch-handle').attr('style','transition-duration: 0.2s; transform: translate(43px, 0px)');
						$('#switch').addClass('mui-active');
					}
					if(data.account_type == 'alipay'){
						$('.ubank').hide();
					}
					$('.addAccount .sure').text('确认修改');
				}else{
					header.init('绑定账户');
					$('.addAccount .sure').text('确认绑定');
				}
		}else{
			layer.msg('网络繁忙，请稍候再试');
		}
	}
);

	footer.init();


window.onload = function(){

	var data=window.sessionStorage.getItem('shop_bindAccount');
	data = JSON.parse(data);
	var ids = 0;
	var types = 0;
	if(data.type == 1){
		ids =data.data1.id;
		types = data.type;
	}else{
		$('.ubank').hide();
	}

	var listT = $('select option');
	for(var i = 0 ;i<listT.length;i++){
		if(listT[i].val() == dataType){
			listT[i].attr('selected','selected');
		}
	}
	$('body').on('blur','#user-name',function(){
		var val = $(this).val();
		if(!val){
			layer.msg('姓名不能为空');
			return false;
		}else if(val.length >=10){
			layer.msg('用户名长度为1~10位');
			return false;
		}
	});
	
	$('body').on('blur','#user-num',function(){
		var val = $(this).val();
		if(!val){
			layer.msg('账号不能为空');
			return false;
		}
	});
	
	$('body').on('change','select',function(){
		var index = $('select option:selected').index('select option');
		if($('select option:selected').val() == 'alipay'){
			$('.ubank').hide();
		}else{
			$('.ubank').show();
		}

	});
	
	$('body').on('click','#switch',function(e){
			
			if (!$(this).hasClass('mui-active')) {
				$(this).attr('style','transition-durantion:0.2s');
				$(this).find('mui-switch-handle').attr('style','transition-duration: 0.2s; transform: translate(43px, 0px)');
				$(this).addClass('mui-active');
			} else {
				$(this).removeClass('mui-active');
			}
	});
	
	$('body').on('blur','#account-mess',function(){
		var val = $(this).val();
		if(!val){
			layer.msg('开户信息不能为空');
			return false;
		}
	});
	
	$('body').on('click','.addAccount .sure',function(){
		var data1={},
		uname = $('#user-name').val(),
		unum = $('#user-num').val(),
		umess = $('#account-mess').val(),
		type = $('select option:selected').val();
		isDefault = $('#switch').hasClass('mui-active') ? 1 :0 ;
		console.log(type);
		if(!uname){
			layer.msg('姓名不能为空');
			return false;
		}else if(!unum){
			layer.msg('账号不能为空');
			return false;
		}else if(type != 'alipay'){
			
			if(!umess){
				layer.msg('开户信息不能为空');
				return false;
			}
		}
		data1={
			account_name : uname,
			account_number : unum,
			account_type : type,
			account_bank_addr : umess,
			is_default : isDefault
		}
		if(types == 1){
			data1={
				account_name : uname,
				account_number : unum,
				account_type : type,
				account_bank_addr : umess,
				is_default : isDefault,
				id : ids
			}
			console.log(data1);
			app.ajax(
				'/Member/Shop/cash_withdrawal_account_add',
				data1,
				function(res){
					if(res.code == 200){
						layer.msg('账户修改成功');
						setTimeout(function(){
							history.go(-1);	
						},1000);

					}
				}
			);
		}else{
			app.ajax(
				'/Member/Shop/cash_withdrawal_account_add',
				data1,
				function(res){
					if(res.code == 200){
						layer.msg('账户添加成功');
						setTimeout(function(){
							history.go(-1);	
						},1000);
					}
				}
			);
		}
		
	});
}
